import Vue from 'vue'
import Vuex from 'vuex'
//导入日志插件
import createLogger from 'vuex/dist/logger'
//导入持久化插件
import createPersistedState from "vuex-persistedstate";
import { shop_fav, shop_favlist, shop_unfav, shopcart_list, shopcart_clear, shopcart_add, shopcart_sub, address_list } from '@/axios/api';
//轻提示
import { Toast } from 'vant';
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    collectlist: [],
    shopcartlist: [],
    addressList: [],

  },
  getters: {
    goodsNum(state) {
      var num = 0
      state.shopcartlist.forEach((item) => {
        num += item.count

      })
      return num
    },
    goodsPrice(state) {
      var price = 0
      state.shopcartlist.forEach((item) => {
        price += parseInt(item.count * item.min_price)
      })
      return price
    }

  },
  mutations: {
    collect(state, payload) { //收藏一个店铺
      state.collectlist.push(payload);

    },
    cancel_collect(state, payload) { //取消收藏一个店铺
      var index = state.collectlist.findIndex((item) => {
        return item.mtWmPoiId == payload.mtWmPoiId;
      })
      if (index != -1) {
        state.collectlist.splice(index, 1);
      }
    },
    save_favlist(state, payload) {  //保存收藏列表
      state.collectlist = payload

    },
    save_address(state, payload) {
      state.addressList = payload

    },

    save_shopcartlist(state, payload) { //保存购物车列表
      state.shopcartlist = payload;
    },
    clear(state) { //清空购物车
      state.shopcartlist = []
    },
    shopcart_add(state, payload) {   //商品数量加1
      var index = state.shopcartlist.findIndex((item) => {
        return item.id == payload.id;
      })
      if (index != -1) {
        state.shopcartlist[index].count++;
      } else {
        //深拷贝商品对象，对深拷贝的对象添加count属性，值为1
        var good = JSON.parse(JSON.stringify(payload))
        good.count = 1
        state.shopcartlist.push(good)
      }
    },
    shopcart_sub(state, payload) {
      var index = state.shopcartlist.findIndex((item) => {
        return item.id == payload.id;
      })
      if (state.shopcartlist[index].count > 0) {
        state.shopcartlist[index].count--;
        if (state.shopcartlist[index].count == 0) {
          state.shopcartlist.splice(index, 1)
        }
      }
    },

  },
  actions: {
    request_favlist(store) {//发送请求获取收藏的店铺列表的异步方法
      //获取用户的id
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      //发请求
      shop_favlist({ userid }).then((res) => {
        store.commit('save_favlist', res.data.list)
      })
    },
    request_address_list(store) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      address_list({ userid }).then((res) => {
        store.commit('save_address', res.data.list)

      })

    },
    request_fav(store, payload) { //发请求收藏列表
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shop_fav({ userid, shopid: payload.mtWmPoiId }).then((res) => {
        if (res.data.code == 200) {
          //调用同步方法。收藏到vuex
          store.commit('collect', payload);
          Toast.success('收藏成功！')
        }
      })
    },

    request_unfav(store, payload) { //发请求取消收藏列表
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shop_unfav({ userid, shopid: payload.mtWmPoiId }).then((res) => {
        if (res.data.code == 200) {
          store.commit('cancel_collect', payload);
          Toast.success('取消收藏成功！')
        }
      })
    },
    request_shopcartlist(store) {  //发请求购物车列表
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shopcart_list({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('save_shopcartlist', res.data.list)

        }
      })
    },
    request_clear(store) {  //发请求清空列表
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shopcart_clear({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('clear');
        }
      })
    },
    request_shopcart_add(store, payload) { //发请求商品数量加1
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shopcart_add({ userid, goodid: payload.id }).then((res) => {
        if (res.data.code == 200) {
          store.commit('shopcart_add', payload);
        }
      })
    },

    request_shopcart_sub(store, payload) { //发请求商品数量减1
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shopcart_sub({ userid, goodid: payload.id }).then((res) => {
        if (res.data.code == 200) {
          store.commit('shopcart_sub', payload);
        }
      })
    }

  },
  modules: { //分模块管理state中的数据

  },
  //配合其他插件 管理vuex中state内部的数据
  plugins: [createLogger(), createPersistedState()]
})
